package com.uwlaser.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.DataMaterialBatch;
import com.uwlaser.domain.DataMaterialSingle;
import com.uwlaser.service.mapper.DataMaterialSingleMapper;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DataMaterialSingleHistory;
import com.uwlaser.service.DataMaterialSingleHistoryService;
import com.uwlaser.service.dto.DataMaterialSingleHistoryDto;
import com.uwlaser.service.dto.DataMaterialSingleHistoryQueryParam;
import com.uwlaser.service.mapper.DataMaterialSingleHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DataMaterialSingleHistoryService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataMaterialSingleHistoryServiceImpl extends CommonServiceImpl<DataMaterialSingleHistoryMapper, DataMaterialSingleHistory> implements DataMaterialSingleHistoryService {

    // private final RedisUtils redisUtils;
    private final DataMaterialSingleHistoryMapper dataMaterialSingleHistoryMapper;
    private final DataMaterialSingleMapper dataMaterialSingleMapper;

    @Override
    public PageInfo<DataMaterialSingleHistoryDto> queryAll(DataMaterialSingleHistoryQueryParam query, Pageable pageable) {
        IPage<DataMaterialSingleHistory> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DataMaterialSingleHistory> page = dataMaterialSingleHistoryMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DataMaterialSingleHistoryDto.class);
    }

    @Override
    public List<DataMaterialSingleHistoryDto> queryAll(DataMaterialSingleHistoryQueryParam query){
        return ConvertUtil.convertList(dataMaterialSingleHistoryMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DataMaterialSingleHistoryDto.class);
    }

    @Override
    public DataMaterialSingleHistory getById(Integer id) {
        return dataMaterialSingleHistoryMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DataMaterialSingleHistoryDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DataMaterialSingleHistoryDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DataMaterialSingleHistoryDto resources) {
        DataMaterialSingleHistory entity = ConvertUtil.convert(resources, DataMaterialSingleHistory.class);
        return dataMaterialSingleHistoryMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DataMaterialSingleHistoryDto resources){
        DataMaterialSingleHistory entity = ConvertUtil.convert(resources, DataMaterialSingleHistory.class);
        int ret = dataMaterialSingleHistoryMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return dataMaterialSingleHistoryMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recover(List<DataMaterialSingleHistoryDto> resources) {
        int result = 0;
        // 删除物料单体码历史 和 新增物料单体码
        result = dataMaterialSingleHistoryMapper.deleteBatchIds(resources.stream().map(DataMaterialSingleHistoryDto::getId).collect(Collectors.toSet()));
        resources.forEach( dataMaterialSingle -> {
            dataMaterialSingle.setId(null);
            dataMaterialSingleMapper.insert(ConvertUtil.convert(dataMaterialSingle,DataMaterialSingle.class));
        });
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DataMaterialSingleHistoryDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DataMaterialSingleHistoryDto dataMaterialSingleHistory : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", dataMaterialSingleHistory.getProductSn());
              map.put("产品型号", dataMaterialSingleHistory.getProductCode());
              map.put("产品类型", dataMaterialSingleHistory.getProductType());
              map.put("订单编号", dataMaterialSingleHistory.getOrderCode());
              map.put("工单编号", dataMaterialSingleHistory.getWorkOrderCode());
              map.put("产线编号", dataMaterialSingleHistory.getLienCode());
              map.put("工段编号", dataMaterialSingleHistory.getSectionCode());
              map.put("工位编号", dataMaterialSingleHistory.getStationCode());
              map.put("工序编号", dataMaterialSingleHistory.getOperationCode());
              map.put("设备编号", dataMaterialSingleHistory.getDeviceCode());
              map.put("物料编号", dataMaterialSingleHistory.getMaterialSn());
              map.put("物料名称", dataMaterialSingleHistory.getMaterialName());
              map.put("物料编码", dataMaterialSingleHistory.getMaterialCode());
              map.put("使用数量", dataMaterialSingleHistory.getQuantity());
              map.put("扫描顺序", dataMaterialSingleHistory.getScanNumber());
              map.put("物料单位", dataMaterialSingleHistory.getMaterialUnit());
              map.put("过站ID", dataMaterialSingleHistory.getPassStationId());
              map.put("创建人", dataMaterialSingleHistory.getCreateBy());
              map.put("创建时间", dataMaterialSingleHistory.getCreateTime());
              map.put("更新人", dataMaterialSingleHistory.getUpdateBy());
              map.put("更新时间", dataMaterialSingleHistory.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
